package cn.jsu.oj.dispatchServer.satellite.config;

import cn.jsu.oj.dispatchServer.satellite.factory.Factory;
import cn.jsu.oj.dispatchServer.satellite.factory.HandlerFactory;
import cn.jsu.oj.dispatchServer.satellite.factory.PackNoFactory;
import cn.jsu.oj.dispatchServer.satellite.factory.StarterFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.concurrent.ExecutorService;

/**
 * 工厂配置
 *
 * @author shan
 * @date 2022/06/29
 */
@Slf4j
@Configuration("factoryConfig")
@DependsOn({"springContext", "executorService"})
public class FactoryConfig {

    @Autowired
    ExecutorService executorService;

    /**
     * 注册处理器工厂bean
     *
     * @return {@link Factory }
     */
    @Bean("handlerFactory")
    @DependsOn("springContext")
    Factory initHandlerFactory() {
        Factory factory = new HandlerFactory();
        executorService.execute(() -> {
            factory.build();
            log.info("bean: 处理器工厂配置完成......");
        });
        return factory;
    }

    /**
     * 注册包编号工厂
     *
     * @return {@link Factory }
     */
    @Bean("packNoFactory")
    @DependsOn("serverPatrol")
    Factory initPackNoFactory() {
        Factory factory = new PackNoFactory();
        executorService.execute(() -> {
            factory.build();
            log.info("bean: 包编号工厂构造完成......");
        });
        return factory;
    }

    /**
     * 注册启动器工厂
     *
     * @return {@link Factory }
     */
    @Bean("starterFactory")
    @DependsOn({"nettyService", "scheduleService", "satelliteServer", "scheduler"})
    Factory initStarter() {
        Factory factory = new StarterFactory();
        executorService.execute(() -> {
            factory.build();
            log.info("bean: 启动器工厂构造完成......");
        });
        return factory;
    }
}
